﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Crm.Sdk.Metadata;
using Microsoft.Crm.Sdk;

namespace Streamsol.Crm.XrmDevelopmentToolkit
{   
    [global::System.AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
    public sealed class XrmFieldMappingAttribute : Attribute
    {
        // See the attribute guidelines at 
        //  http://go.microsoft.com/fwlink/?LinkId=85236

        readonly string attributeName;
        bool _isValidForUpdate = true;
        bool _isValidForCreate = true;

        public XrmFieldMappingAttribute(string attributeName)
            : this(attributeName, AttributeType.String)
        { }

        public XrmFieldMappingAttribute(string attributeName, AttributeType attributeType)
        {
            this.attributeName = attributeName;
            this.CrmType = attributeType;
        }

        public string AttributeName
        {
            get { return attributeName; }
        }

        public AttributeType CrmType
        {
            get;
            set;
        }

        /// <summary>
        /// Specify this attribute is valid for create. Default is true;
        /// </summary>
        public bool IsValidForCreate
        {
            get { return _isValidForCreate; }
            set { this._isValidForCreate = value; }
        }

        /// <summary>
        /// Specify this attribute is valid for updates. Default is true;
        /// </summary>
        public bool IsValidForUpdate
        {
            get { return _isValidForUpdate; }
            set { this._isValidForUpdate = value; }
        }

        /// <summary>
        /// Specify this attribute is valid for updates. Default is true;
        /// </summary>
        public bool IsReadOnly
        {
            get { return !IsValidForUpdate && !IsValidForCreate; }
            set
            {
                IsValidForCreate = !value;
                IsValidForUpdate = !value;
            }
        }
    }
}
